home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_02 / 9n02090a < prev    next >
Text File  |  1990-12-16  |  8KB  |  280 lines

  1.  
  2. /*
  3.  *    10-Oct-1990 - created
  4.  */
  5.  
  6. /***********************************************************
  7.  *
  8.  *    Module:   test.c
  9.  *
  10.  *    Purpose:  Test code driver for olist.c
  11.  *
  12.  **********************************************************/
  13.  
  14.  
  15. #include "local.h"
  16. #include "olist.h"
  17.  
  18.  
  19. #define MAX_INSTRING    100
  20. #define TEST_TITLE  "\n Test Ordered Lists Using Skip Lists\n\n"
  21.  
  22.  
  23. static int      test_get_item ();
  24. static void     test_insert_many ();
  25. static bool     test_ok ();
  26.  
  27.  
  28. typedef struct menu_struct
  29. {
  30.     int         item_id;
  31.     char        *item_text;
  32. } MENU_ITEM;
  33.  
  34. enum menu_item_id
  35. {
  36.     uint_item_id,
  37.     string_item_id,
  38.     show_uint_item_id,
  39.     show_string_item_id,
  40.     done_item_id,
  41.     num_menu_items
  42. };
  43.  
  44. MENU_ITEM       *test_menu[num_menu_items];
  45.  
  46. MENU_ITEM uint_item = 
  47.            { uint_item_id,   "Find a value" };
  48. MENU_ITEM string_item =
  49.            { string_item_id, "Find a string" };
  50. MENU_ITEM show_uint_item =
  51.           { show_uint_item_id,   "Show value data" };
  52. MENU_ITEM show_string_item =
  53.           { show_string_item_id, "Show string data" };
  54. MENU_ITEM done_item =
  55.           { done_item_id,    "Done" };
  56.  
  57. char    instring[MAX_INSTRING + 1];
  58.  
  59. OLIST   uint_o_list;
  60. OLIST   ptr_o_list;
  61.  
  62. /***********************************************************
  63.  *                          main
  64.  **********************************************************/
  65.  
  66. main (argc, argv, envp)
  67.     int   argc;
  68.     char  **argv;
  69.     char  **envp;
  70. {
  71.     int         item_id;
  72.     uint        u_key;
  73.     char        *p_key;
  74.     char        *client_data;
  75.                                /* fill menu items */
  76.     test_menu[uint_item.item_id] = &uint_item;
  77.     test_menu[string_item.item_id] = &string_item;
  78.     test_menu[show_uint_item.item_id] = &show_uint_item;
  79.     test_menu[show_string_item.item_id] = &show_string_item;
  80.     test_menu[done_item.item_id] = &done_item;
  81.                                 /* make the skip lists */
  82.     uint_o_list = olistu_new();
  83.     ptr_o_list = olistp_new(strcmp);
  84.                                 /* fill list initially */
  85.     test_insert_many();
  86.                                 /* repeat until done */
  87.     do {
  88.                                 /* get the next menu item */
  89.         item_id = test_get_item();
  90.         switch (item_id)
  91.         {
  92.           default:
  93.           case uint_item_id:
  94.             printf("Find Unsigned Integer key: ");
  95.             gets(instring);
  96.             u_key = (uint)atoi(instring);
  97.             client_data = olistu_find(uint_o_list, u_key);
  98.             if (test_ok())
  99.             {
  100.                 printf("  Found uint Key: %6d", u_key);
  101.                 printf("  data: \"%s\"", (char *)client_data);
  102.                 printf("\n\n  Number of comparisons: %d\n",
  103.                        olistu_compares(uint_o_list, u_key));
  104.             }
  105.             break;
  106.  
  107.           case string_item_id:
  108.             printf("Find String key: ");
  109.             gets(instring);
  110.             p_key = instring;
  111.             client_data = olistp_find(ptr_o_list, p_key);
  112.             if (test_ok())
  113.             {
  114.                 printf("  Found ptr Key: \"%s\"", instring);
  115.                 printf("  data: \"%s\"", (char *)client_data);
  116.                 printf("\n\n  Number of comparisons: %d\n",
  117.                        olistp_compares(ptr_o_list, p_key));
  118.             }
  119.             break;
  120.  
  121.           case show_uint_item_id:
  122.             printf("\nUnsigned int key:\n");
  123.             client_data = olistu_get_first(uint_o_list, u_key);
  124.             if (test_ok()) printf("  First key: %d\n", u_key);
  125.  
  126.             client_data = olistu_get_last(uint_o_list, u_key);
  127.             if (test_ok()) printf("  Last key: %d\n", u_key);
  128.  
  129.             client_data = olistu_get_current(uint_o_list, u_key);
  130.             if (test_ok()) printf("  Current key: %d\n", u_key);
  131.  
  132.             client_data = olistu_get_next(uint_o_list, u_key);
  133.             if (test_ok()) printf("  Next key: %d\n", u_key);
  134.             break;
  135.  
  136.           case show_string_item_id:
  137.             printf("\nString key:\n");
  138.             client_data = olistp_get_first(ptr_o_list, p_key);
  139.             if (test_ok()) printf("  First key: \"%s\"\n",
  140.                        p_key);
  141.  
  142.             client_data = olistp_get_last(ptr_o_list, p_key);
  143.             if (test_ok()) printf("  Last key: \"%s\"\n", p_key);
  144.  
  145.             client_data = olistp_get_current(ptr_o_list, p_key);
  146.             if (test_ok()) printf("  Current key: \"%s\"\n",
  147.                        p_key);
  148.  
  149.             client_data = olistp_get_next(ptr_o_list, p_key);
  150.             if (test_ok()) printf("  Next key: \"%s\"\n", p_key);
  151.             break;
  152.  
  153.           case done_item_id:
  154.             break;
  155.         }
  156.     } while (item_id != done_item_id);
  157.     printf("\nDone.\n");
  158.     exit(0);
  159. }
  160.  
  161. /**********************************************************
  162.  *
  163.  *                     test_get_item
  164.  *
  165.  *  Get a menu item from the user and return its item
  166.  *  id number.
  167.  *
  168.  **********************************************************/
  169.  
  170. static int test_get_item ()
  171. {
  172.     register int        ii;
  173.                                 /* show the test menu */
  174.     printf(TEST_TITLE);
  175.     for (ii = 0; ii < num_menu_items; ++ii)
  176.     {
  177.         printf("  %-2d   %s\n",
  178.                test_menu[ii]->item_id + 1,
  179.                test_menu[ii]->item_text);
  180.     }
  181.                                 /* get the menu item number */
  182.     printf("\nItem number: ");
  183.     gets(instring);
  184.                                 /* return the item id */
  185.     return(atoi(instring) - 1);
  186. }
  187.  
  188. /**********************************************************
  189.  *
  190.  *                   test_insert_many
  191.  *
  192.  *  Insert many (sequential) keys to each olist.
  193.  *
  194.  **********************************************************/
  195.  
  196. static void test_insert_many ()
  197. {
  198.     register int ii;
  199.     int         num_items;
  200.     uint        u_key;
  201.     char        *p_key;
  202.     char        *client_data;
  203.                                 /* get number of items */
  204.     printf("\nNumber of items to insert: ");
  205.     gets(instring);
  206.     num_items = atoi(instring);
  207.     printf("\nFilling the skip lists...");
  208.                                 /* fill each data item */
  209.     for (ii = 1; ii <= num_items; ++ii)
  210.     {
  211.                                 /* unsigned int key */
  212.         sprintf(instring, "data %d", ii);
  213.         u_key = (uint)ii;
  214.         client_data = strdup(instring);
  215.         if (client_data != NULL)
  216.         {
  217.             olistu_insert(uint_o_list, u_key, client_data);
  218.                                 /* string key */
  219.             sprintf(instring, "key_%d", ii);
  220.             p_key = strdup(instring);
  221.             if (p_key != NULL)
  222.             {
  223.                 olistp_insert(ptr_o_list, p_key, client_data);
  224.             }
  225.         }
  226.     }
  227.                                 /* show list reports */
  228.     printf("\n\n -- Skip List with unsigned int keys --\n");
  229.     olist_report(uint_o_list);
  230.     printf("\n Please press Enter to continue...");
  231.     gets(instring);
  232.     printf("\n -- Skip List with string keys --\n");
  233.     olist_report(ptr_o_list);
  234. }
  235.  
  236. /**********************************************************
  237.  *
  238.  *                       test_ok
  239.  *
  240.  **********************************************************/
  241.  
  242. static bool test_ok ()
  243. {
  244.     bool        ok = FALSE;
  245.  
  246.     switch (olist_errno)
  247.     {
  248.       default:
  249.       case OLIST_SUCCESS:
  250.         ok = TRUE;
  251.         printf("\n    *** Success ***");
  252.         break;
  253.       case OLIST_INVALID:
  254.         printf("\n *** %s ***\n", OLIST_INVALID_STR);
  255.         break;
  256.       case OLIST_NO_MEMORY:
  257.         printf("\n *** %s ***\n", OLIST_NO_MEMORY_STR);
  258.         break;
  259.       case OLIST_EMPTY:
  260.         printf("\n *** %s ***\n", OLIST_EMPTY_STR);
  261.         break;
  262.       case OLIST_INVALID_TYPE:
  263.         printf("\n *** %s ***\n", OLIST_INVALID_TYPE_STR);
  264.         break;
  265.       case OLIST_NOT_FOUND:
  266.         printf("\n *** %s ***\n", OLIST_NOT_FOUND_STR);
  267.         break;
  268.       case OLIST_KEY_EXISTS:
  269.         printf("\n *** %s ***\n", OLIST